//
//  MainTabBar.swift
//  SineWeibo
//
//  Created by keshuaiguo on 16/1/14.
//  Copyright © 2016年 keshuaiguo. All rights reserved.
//

import UIKit

class MainTabBar: UITabBar {

    override init(frame: CGRect) {
        
        super.init(frame: frame)
        //添加加号按钮
        addSubview(plusBtn)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //修改tabbar子视图的位置 和大小
    override func layoutSubviews() {
        
        super.layoutSubviews()
        
        //定义item的宽度
        let w = self.bounds.width / 5
        let h = self.bounds.height
        let rect = CGRect(x: 0, y: 0, width: w, height: h)
        //定义索引
        var index : CGFloat = 0
        //1.遍历子视图
        
        for subview in subviews {
            
            //获取所有的UITabBarButton
            if subview.isKindOfClass(NSClassFromString("UITabBarButton")!) {
                
//                print(subview)
                subview.frame = CGRectOffset(rect, index * w, 0)
                
                //                index == 1 ? (index++) : index + 2
                //
                //                index++
                index += (index == 1 ? 2 : 1)
                
                //设置加号按钮的frame
                plusBtn.frame = CGRectOffset(rect, 2 * w, 0)
            }
        }
    }
    //懒加载加号按钮
    lazy var plusBtn : UIButton = {
        
        let btn = UIButton()
        //设置背景图片
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: .Normal)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: .Highlighted)
        
        //设置按钮图片
        btn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: .Normal)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: .Highlighted)
        //自适应大小
        btn.sizeToFit()
        
        return btn
    }()
}